﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;
using System.Text.RegularExpressions;

namespace ECCSystem
{
    public partial class frmTeamGoal : Form
    {
		private BizRule.ClsTeamGoal objBRMC;
		private int intMode = 0;
        public frmTeamGoal()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
				if (Check() == false)
				{
					return;
				}
				if (intMode == 0)
				{
					objBRMC = new ECCSystem.BizRule.ClsTeamGoal();
					objBRMC.AddTeamGoal(txtEfficiency.Text.ToString().Trim(),
										txtFactAchievement.Text.ToString().Trim(),
										txtDefect.Text.ToString().Trim(), txtDefectQuotietyrash.Text.Trim(), dtpFrom.Value);

				}
				else
				{
					objBRMC = new ECCSystem.BizRule.ClsTeamGoal();
					objBRMC.UpdateTeamGoal(txtEfficiency.Text.ToString().Trim(),
										txtFactAchievement.Text.ToString().Trim(),
										txtDefect.Text.ToString().Trim(),
										txtDefectQuotietyrash.Text.Trim(),
										dtpFrom.Value,
										dgList.SelectedRows[0].Cells["TeamGoal_KeyID"].Value.ToString()
										);
					dgList.Enabled = true;
					intMode = 0;
				}

				this.btnDelete.Enabled = true;
				this.btnUpdate.Enabled = true;
				this.dgList.Enabled = true;
				txtEfficiency.Text = "";
				txtFactAchievement.Text = "";
				txtDefect.Text = "";
				txtDefectQuotietyrash.Text = "";
				dtpFrom.Value = DateTime.Now;
				this.BindDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //wether have selected row,if have not selected row, pop-up prompt
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                else
                {
                    if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        objBRMC = new ECCSystem.BizRule.ClsTeamGoal();
                        objBRMC.DeleteTeamGoal(Convert.ToString(dgList.SelectedRows[0].Cells[0].Value));
                        this.BindDataGrid();
						txtEfficiency.Text = "";
						txtFactAchievement.Text = "";
						txtDefect.Text = "";
						txtDefectQuotietyrash.Text = "";
						dtpFrom.Value = DateTime.Now;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //bind data
        private void BindDataGrid()
        {
            DataSet objDS = new DataSet();
            objBRMC = new ECCSystem.BizRule.ClsTeamGoal();
            objDS = (DataSet)objBRMC.GetList();
            //Don't show blank row
            objDS.Tables[0].DefaultView.AllowNew = false;
            dgList.DataSource = objDS.Tables[0];

			if (dgList.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdate.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				txtEfficiency.Text = objDS.Tables[0].Rows[0]["TeamGoal_Efficiency"].ToString();
				txtFactAchievement.Text = objDS.Tables[0].Rows[0]["TeamGoal_FactAchievement"].ToString();
				txtDefect.Text = objDS.Tables[0].Rows[0]["TeamGoal_Defect"].ToString();
				txtDefectQuotietyrash.Text = objDS.Tables[0].Rows[0]["TeamGoal_DefectQuotietyrash"].ToString();
				dtpFrom.Value = Convert.ToDateTime(objDS.Tables[0].Rows[0]["Goal_Month"]);
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdate.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
        }

        //Validate Data 
        private bool CheckInputItem(string MailCategoryName)
        {
            if (MailCategoryName == "")
            {
                MessageBox.Show("カテゴリを入力してください！", "ECC Information");
                return false;
            }
            return true;
        }

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (dgList.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			dgList.Enabled = false;
			//txtMailCategory.Text = Convert.ToString(dgList.SelectedRows[0].Cells["MailCategory"].Value);
			//txtComment.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Category_Memo"].Value);
			//txtPoint.Text = Convert.ToString(dgList.SelectedRows[0].Cells["Point"].Value);
			intMode = 1;

			this.btnDelete.Enabled = false;
			this.btnUpdate.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			txtEfficiency.Text = "";
			txtFactAchievement.Text = "";
			txtDefect.Text = "";
			txtDefectQuotietyrash.Text = "";
			intMode = 0;

			if (dgList.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdate.Enabled = true;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dgList.Rows[0].Selected = true;

				txtEfficiency.Text = Convert.ToString(dgList.CurrentRow.Cells["TeamGoal_Efficiency"].Value);
				txtFactAchievement.Text = Convert.ToString(dgList.CurrentRow.Cells["TeamGoal_FactAchievement"].Value);
				txtDefect.Text = Convert.ToString(dgList.CurrentRow.Cells["TeamGoal_Defect"].Value);
				txtDefectQuotietyrash.Text = Convert.ToString(dgList.CurrentRow.Cells["TeamGoal_DefectQuotietyrash"].Value);
				dtpFrom.Value = Convert.ToDateTime(dgList.CurrentRow.Cells["Goal_Month"].Value);
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdate.Enabled = false;
				this.dgList.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

		}

		private bool Check()
		{
			if (txtEfficiency.Text.Trim() == "")
			{
				MessageBox.Show("チーム効率目標入力してください！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtEfficiency.Text.Trim(), 50) == false)
			{
				MessageBox.Show("チーム効率目標入力长度超出！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtFactAchievement.Text.Trim(), 50) == false)
			{
				MessageBox.Show("実績入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtDefect.Text.Trim(), 50) == false)
			{
				MessageBox.Show("不備目标入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (clsUICheck.CheckLen(txtDefectQuotietyrash.Text.Trim(), 50) == false)
			{
				MessageBox.Show("不備率入力長さは長すぎ！", "ECC Information");
				return false;
			}

			if (intMode == 1)
			{
				for (int i = 0; i < this.dgList.Rows.Count; i++)
				{
					if (dtpFrom.Value.ToString("yyyy/MM").Trim() == Convert.ToDateTime(dgList.Rows[i].Cells["Goal_Month"].Value).ToString("yyyy/MM").Trim() &&
						i != dgList.CurrentRow.Index)
					{
						MessageBox.Show("本日付のデータは既に存在しています。!", "ECC Information");
						return false;
					}
				}

				return true; ;
			}

			for (int i = 0; i < this.dgList.Rows.Count; i++)
			{
				if (dtpFrom.Value.ToString("yyyy/MM").Trim() == Convert.ToDateTime(dgList.Rows[i].Cells["Goal_Month"].Value).ToString("yyyy/MM").Trim())
				{
					MessageBox.Show("本日付のデータは既に存在しています", "ECC Information");
					return false;
				}
			}

			return true;
		}

		private void dgList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			txtEfficiency.Text = "";
			txtFactAchievement.Text = "";
			txtDefect.Text = "";
			txtDefectQuotietyrash.Text = "";
			dtpFrom.Value = DateTime.Now;
			if (dgList.SelectedRows.Count <= 0)
			{
				return;
			}

			txtEfficiency.Text = Convert.ToString(dgList.SelectedRows[0].Cells["TeamGoal_Efficiency"].Value);
			txtFactAchievement.Text = Convert.ToString(dgList.SelectedRows[0].Cells["TeamGoal_FactAchievement"].Value);
			txtDefect.Text = Convert.ToString(dgList.SelectedRows[0].Cells["TeamGoal_Defect"].Value);
			txtDefectQuotietyrash.Text = Convert.ToString(dgList.SelectedRows[0].Cells["TeamGoal_DefectQuotietyrash"].Value);
			dtpFrom.Value = Convert.ToDateTime(dgList.SelectedRows[0].Cells["Goal_Month"].Value);	
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			txtEfficiency.Text = "";
			txtFactAchievement.Text = "";
			txtDefect.Text = "";
			txtDefectQuotietyrash.Text = "";
			dtpFrom.Value = DateTime.Now;

			this.btnDelete.Enabled = false;
			this.btnUpdate.Enabled = false;
			this.dgList.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 0;
		}

		private void frmTeamGoal_Load(object sender, EventArgs e)
		{
			try
			{
				this.BindDataGrid();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "ECC Catch Error");
			}
		}

		private void frmTeamGoal_FormClosing(object sender, FormClosingEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}
    }
}